package com.fauks.study.msdata.controller;

import com.fauks.study.msdata.mapper.EmpMapper;
import com.fauks.study.msdata.mapper.PersonMapper;
import com.fauks.study.msdata.pojo.Emp;
import com.fauks.study.msdata.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private PersonMapper personMapper;

    @GetMapping("/emp1/{id}")
    public Emp empByJdbc(@PathVariable("id") String id) {
        System.out.println(id);
        return jdbcTemplate.queryForObject("select id,lastName,email,gender from employee where id=?"
                , new BeanPropertyRowMapper<>(Emp.class), id);
    }

    @GetMapping("/emp/{id}")
    public Emp empByMybatis(@PathVariable("id") Integer id) {
        return empMapper.getEmpById(id);
    }

    @GetMapping("/person/{id}")
    public Person personByMybatis(@PathVariable("id") Integer id) {
        return personMapper.selectById(id);
    }

    @GetMapping("/emp/add")
    public String empAddByMybatis(Emp emp) {
        empMapper.insert(emp);
        return "ok";
    }

    @GetMapping("/emp/update")
    public String empUpdateByMybatis(Emp emp) {
        empMapper.updateById(emp);
        return "ok";
    }
}
